package main

import "fmt"

func main() {
	// 对slice 求和
	nums := []int{2, 3, 4}
	sum := 0
	for _, num := range nums {
		sum += num
	}
	fmt.Println("2,3,4求和：", sum)

	//range 循环slice,第一个值为键，第二个为值
	for i, num := range nums {
		if i == 0 {
			fmt.Println(num)
		}
		if num == 4 {
			fmt.Println(i)
		}
	}

	//range迭代map对象
	m := map[string]string{"nanfeng": "mingquan", "zhangsan": "wangwu"}

	for key, val := range m {
		//fmt.Println(val)
		fmt.Printf("%s -> %s ", key, val)
	}

	//只遍历map的键
	for key := range m {
		fmt.Println("key:", key)
	}

	//遍历字符串 值是该字符对应的unicode码点
	for i, c := range "mingquan" {
		fmt.Println(i, c)
	}
}
